package com.example.demo1.support;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.math.BigDecimal;

@JsonComponent
public class MoneyDeserializer extends StdDeserializer<Money> {

   protected MoneyDeserializer(){
       super(Money.class);
   }

    @Override
    public Money deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException {
       // USD 8.00 ->美元的8元
        String strValue = jsonParser.getValueAsString();
        //BigDecimal numberValue = jsonParser.getDecimalValue();
        return Money.of(CurrencyUnit.of("CNY"),Double.parseDouble(strValue));
    }
}
